<!DOCTYPE html>
<html>

	<head>

		<meta name="viewport" content="width=device-width-10, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
		<meta charset="utf-8" />
		<title>转换为机型码</title>

		<link rel="stylesheet" href="../css/mdui.css">
		<script src="../js/mdui.js"></script>
		<script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
		<script type="text/javascript" src="../js/clipboard.min.js"></script>
		<script type="text/javascript" src="../js/dabaiutils.js"></script>

		<script type="text/javascript" src="../js/model-utils.js"></script>

		<link rel="icon" href="../img/bitbug_favicon.ico">
		<link rel="shortcut icon" href="../img/bitbug_favicon.ico">

	</head>

	<body class="mdui-theme-primary-indigo mdui-theme-accent-indigo">

		<!--appbar-->
		<div class="mdui-appbar">
			<div class="mdui-toolbar mdui-color-theme">
				<a href="../index.html" class="mdui-btn mdui-btn-icon"><i class="mdui-icon material-icons">&#xe5c4;</i></a>
				<a href="javascript:;" class="mdui-typo-headline">转换为机型码</a>

			</div>
		</div>

		<div class="mdui-card mdui-m-a-2">

			<!-- 卡片的内容 -->
			<div class="mdui-card-content">

				<div class="mdui-textfield">
					<label class="mdui-textfield-label">model</label>
					<input class="mdui-textfield-input" type="text" id="te1" />
				</div>

				<div class="mdui-textfield">
					<label class="mdui-textfield-label">brand</label>
					<input class="mdui-textfield-input" type="text" id="te2" />
				</div>

				<div class="mdui-textfield">
					<label class="mdui-textfield-label">manufacturer</label>
					<input class="mdui-textfield-input" type="text" id="te3" />
				</div>

				<div class="mdui-textfield">
					<label class="mdui-textfield-label">product</label>
					<input class="mdui-textfield-input" type="text" id="te4" />
				</div>

				<div class="mdui-textfield">
					<label class="mdui-textfield-label">device</label>
					<input class="mdui-textfield-input" type="text" id="te5" />
				</div>

				<!-- 卡片的按钮 -->
				<div class="mdui-card-actions">

					<button id="bu5" class="mdui-btn mdui-btn-icon mdui-float-right" mdui-tooltip="{content: '清空', position: 'top'}">
        				<i class="mdui-icon material-icons">&#xe0b8;</i>
      				</button>

					<button id="bu1" class="mdui-btn mdui-btn-icon mdui-float-right" mdui-tooltip="{content: '转换为机型码', position: 'top'}">
        				<i class="mdui-icon material-icons">&#xe876;</i>
      				</button>

				</div>

				<div class="mdui-textfield">
					<label class="mdui-textfield-label">机型码</label>
					<input class="mdui-textfield-input" type="text" id="te_code" />
				</div>

				<!-- 卡片的按钮 -->
				<div class="mdui-card-actions">

					<button id="bu4" class="mdui-btn mdui-btn-icon mdui-float-right" mdui-tooltip="{content: '转化为链接', position: 'top'}">
        				<i class="mdui-icon material-icons">&#xe157;</i>
      				</button>

					<button id="bu3" class="mdui-btn mdui-btn-icon mdui-float-right" mdui-tooltip="{content: '导入App', position: 'top'}">
        				<i class="mdui-icon material-icons">&#xe895;</i>
      				</button>

					<button id="bu2" class="mdui-btn mdui-btn-icon mdui-float-right" mdui-tooltip="{content: '复制', position: 'top'}">
        				<i class="mdui-icon material-icons">&#xe14d;</i>
      				</button>

				</div>
			</div>

		</div>

		<p class="mdui-m-a-2" style="color: #9FA8DA;font-size: 12px;">
			<a href="https://www.coolapk.com/apk/241250" target="_blank">下载机型更改App</a>
		</p>

	</body>

	<script type="text/javascript">
		var te_code = document.getElementById("te_code");

		var te1 = document.getElementById("te1");
		var te2 = document.getElementById("te2");
		var te3 = document.getElementById("te3");
		var te4 = document.getElementById("te4");
		var te5 = document.getElementById("te5");

		$("#bu1").click(function() {
			if(te1.value.length <= 0 || te2.value.length <= 0 || te3.value.length <= 0 || te4.value.length <= 0 || te5.value.length <= 0) {
				mdui.snackbar({
					message: '5项值均不能为空'
				});
			} else {

				var words = te1.value + "@" + te2.value + "@" + te3.value + "@" + te4.value + "@" + te5.value;

				try {
					te_code.value = window.btoa(words);
					mdui.snackbar({
						message: '转换成功'
					});
				} catch(e) {
					mdui.snackbar({
						message: '转换失败'
					});
				}

			}
		});

		$("#bu2").click(function() {

			if(te_code.value.length <= 0) {
				mdui.snackbar({
					message: '复制失败，机型码为空'
				});
			} else {
				copyText("#bu2", te_code.value, "机型码已复制", "复制失败");
			}

		});

		$("#bu3").click(function() {
			toChangeModelApp(te_code.value);
		});

		$("#bu4").click(function() {
			copyText("#bu4", intoUrl(te_code.value), "转化后的网址已复制", "复制失败");
		});
		$("#bu5").click(function() {
			
			// 含文本、标题和确认按钮回调
			mdui.confirm('确定要清空5项值编辑框嘛？', '提示', function() {
				te1.value = "";
			te2.value = "";
			te3.value = "";
			te4.value = "";
			te5.value = "";

				mdui.snackbar({
					message: '已清空'
				});
			}, function() {}, {
				confirmText: '确认',
				cancelText: '取消'
			});


			
		});
		
		
		
//复制文本
function copyText(buttonId, copyContent, successText, errorText) {
	//需要引入 clipboard.min.js
	var clipboard = new ClipboardJS(buttonId, {
		text: function() {
			return copyContent;
		}
	});

	clipboard.on('success', function(e) {
		mdui.snackbar({
			message: successText
		});
		return true;
	});

	clipboard.on('error', function(e) {
		mdui.snackbar({
			message: errorText
		});
		return false;
	});
	return false;

}
		
		
		function toChangeModelApp(code) {

	if(code.length <= 0) {
		mdui.snackbar({
			message: '跳转失败，机型码为空'
		});
	} else {

		try {
			var words = window.atob(code).split('@');

			if(words.length == 5) {

				window.location.href = "dabai://model?code=" + code;
				mdui.snackbar({
					message: '正在尝试跳转...</br>如果跳转失败，请<a style="color:white;" href="https://www.coolapk.com/apk/241250" target="_blank">下载最新版机型更改App</a>'
				});

			} else {
				mdui.snackbar({
					message: '无效机型码'
				});
			}

		} catch(e) {
			mdui.snackbar({
				message: '无效机型码'
			});
		}

	}

}

function intoUrl(code) {
	if(code.length <= 0) {
		mdui.snackbar({
			message: '转化失败，机型码为空'
		});
		return "";
	} else {

		var codelink = "https://dabai2017.gitee.io/model-change-tool?code=" + code;
		return codelink;
	}

}

function verifyModelCode(code) {
	try {
		var words = window.atob(code).split('@');

		if(words.length == 5) {
			return true;
		} else {
			return false;
		}

	} catch(e) {
		return false;
	}
}

	</script>

</html>